{header}
<div class="card">
  <div class="card-header">
    操作
  </div>
  <div class="card-body">
    <a class="btn btn-primary" href="copywrite_add" role="button">创建订单</a>
  </div>
</div>
<div class="container">
  <div class="row">
    筛选：
    <div class="dropdown" style="margin-right: 10px;">
	    <button class="btn btn-secondary dropdown-toggle" type="button" id="type_btn" data-toggle="dropdown" aria-expanded="false">{type}</button>
      <div class="dropdown-menu" style="max-height: 300px;overflow-y: auto;">
        {type_buttons}
      </div>
    </div>
    <div class="dropdown" id="topic_dropdown">
      <button class="btn btn-secondary dropdown-toggle" type="button" id="topic_btn" data-toggle="dropdown" aria-expanded="false">{group_name}</button>
      <div class="dropdown-menu" id="topic_list" style="max-height: 300px;overflow-y: auto;">
        {topic_buttons}
      </div>
    </div>
    <script type="text/javascript">
      function set_type(value) {
        location.href = '{base_url}copywrite_list?p=1&topic_type=' + value;
      }
      function set_topic(id, number, topic_name) {
        $('#topic_btn').html(number + '-' + topic_name);
        var type = $('#type_btn').html();
        location.href = '{base_url}copywrite_list?p=1&topic_type=' + type + '&group_id=' + id + '&topic_name=' + topic_name;
      }
    </script>
  </div>
</div>
<table class="table table-striped">
  <thead>
    <tr>
      <th scope="col">ID</th>
      <th scope="col">选题</th>
      <th scope="col">文案要求</th>
      <th scope="col">迅雷种子</th>
      <th scope="col">操作</th>
    </tr>
  </thead>
  <tbody id="list">
    {tr}
  </tbody>
</table>
<script type="text/javascript">
  if (ws !== null) {
    ws.addEventListener('message', function (event) {
      var kv = str2kv(event.data);
      var data = event.data;
      if (kv['msg'] !== undefined && kv['msg'].indexOf('"-"') !== -1) {
        var msg_kv = kv['msg'].split('"-"');
        if (msg_kv[0] === 'msg' && msg_kv[1] === 'copywrite_list') {
          $.post('ws_lib/copywrite_list', {
		    skip_number: {skip_number},
		    type: '{type}',
            group_id: '{group_id}',
		    topic_name: '{topic_name}'
          }, function (data) {
            var json = JSON.parse(data);
            $('#list').empty();
            for (var i in json) {
              var tr = $('<tr></tr>');
              var th = $('<th scope="row"></th>');
              th.html(json[i]['id']);
              var td1 = $('<td></td>');
              var td2 = $('<td></td>');
              var td3 = $('<td><a target="_blank" href="{base_url}uploads/bt_file/' + json[i]['bt_file'] + '">下载</a></td>');
              var td4 = $('<td></td>');
              var a = $('<a class="btn btn-primary" target="_blank" href="{base_url}view_copywrite_order.php?id=' + json[i]['id'] + '">查看</a>');
              var btn = $('<button class="btn btn-success" onclick="get_order(' + json[i]['id'] + ')">接单</button>');
              td1.html(json[i]['topic_name']);
              td2.html(json[i]['requirement']);
              td4.append(a, btn);
              tr.append(th, td1, td2, td3, td4);
              $('#list').append(tr);
            }
          });
        }
      } else if (data.indexOf(':') !== -1) {
        var d = data.split(':');
        if (d[0] === 'fail') {
          layer.alert('系统错误，' + d[1], {icon:2});
          return;
        }
      }
    });
  }
  function get_order(id) {
    $.ajax({
      url: '{base_url}library/get_copywrite_order',
      type: 'post',
      data: {
        id: id
      },
      success: function (data) {
        var d = data.split(':');
        if (d.length !== 2) {
          console.error(data);
          layer.alert('服务器返回非法数据', {icon: 2});
          return;
        }
        if (d[0] === 'fail') {
          layer.alert('接单失败:' + d[1], {icon: 2});
          console.error('接单失败:' + d[1]);
        } else if (d[0] === 'success') {
          if (ws !== null && ws.readyState === 1) {
            ws.send('receive_id"="0";"msg"="msg"-"copywrite_list');
            ws.send('receive_id"="{uid}";"msg"="msg"-"task_list');
          }
 	      location.href = '{base_url}task_list?p=1';
        } else {
          console.error(data);
          layer.alert('服务器返回多余数据', {icon: 2});
        }
      },
      error: function (data) {
        layer.alert('接单失败', {icon: 2});
        console.error(data.responseText);
      }
    });
  }
</script>
{nav}
{footer}

